<template>
	<view class="container">
		<view class="nav">
			<view @tap="changeTab(index)" :class="{active:currentTab==index}" class="nav-item"
				v-for="(item,index) in navList" :key="index">{{item.name}}</view>
		</view>
		<view class="container-c">
			<view class="" v-for="item in list">
				<view class="card" v-for="value in item.items">
					<view class="top">
						<text>订单编号:{{value.orderSn}}</text>
						<text>{{item.order.status | statusText}}</text>
					</view>
					<view class="middle">
						<image class="left" :src="value.productPic" mode=""></image>
						<view class="right">
							<view class="top-info">
								<text class="name">{{value.productName}}</text>
								<text class="price">¥{{value.productPrice}}元</text>
							</view>
							<view class="middle-info">
								<text class="">编号{{value.orderId}}</text>
								<text class="price">x{{value.productQuantity}}</text>
							</view>
							<view class="bottom-info" v-for="ele in value.productAttr" :key="ele.id">
								{{ele.key}}:{{ele.value}}
							</view>
							<view v-if="currentTab==2&&value.isReturn==0" class="return" @tap="returnGoods(value.orderId)">
								退货
							</view>
							<view v-if="currentTab==2&&value.isReturn==1" class="return">
								退货中
							</view>
						</view>
					</view>
					<view class="bottom">
						<view class="time">
							时间:{{value.createTime}}
						</view>
						<view class="total">
							合计:{{value.totalPrice}}元
						</view>
					</view>
					<view class="btn" v-if="currentTab==0">
						<view class="cancel" @tap="cancel(value.orderId)">
							取消订单
						</view>
						<view class="paybtn" @tap="confirmPay(value.orderId)">
							付款
						</view>
					</view>
					<view class="btn" v-if="currentTab==2">
						<view class="paybtn" @tap="doReceive(value.orderId)">
							确认收货
						</view>
					</view>
					<view @tap="delHistory(value.orderId)" class="delete" v-if="currentTab==3 && item.order.status==4">
						订单已关闭,删除记录
					</view>
				</view>
			</view>
			<!-- 申请退单 -->
			<view class="" v-if="currentTab == 4">
				<view class="card" v-for="item in list">
					<view class="top">
						<text>订单编号:{{item.orderSn}}</text>
						<text>{{item.status | orderProcessText}}</text>
					</view>
					<view class="middle">
						<image class="left" :src="item.productPic" mode=""></image>
						<view class="right">
							<view class="top-info">
								<text class="name">{{item.productName}}</text>
								<text class="price">¥{{item.returnAmount}}元</text>
							</view>
							<view class="middle-info">
								<text class="name">编号{{item.orderId}}</text>
								<text class="price">x{{item.productCount}}</text>
							</view>
							<view class="bottom-info" v-for="ele in item.productAttr" :key="ele.id">
								{{ele.key}}:{{ele.value}}
							</view>
						</view>
					</view>
					<view class="bottom">
						<view class="time">
							时间:{{item.createTime}}
						</view>
						<view class="total">
							合计:{{item.returnAmount}}元
						</view>
					</view>
				</view>
			</view>
			
		</view>
	</view>
</template>

<script>
	import {
		cancelOrder,
		receiveDone,
		deleteOrder
	} from "@/api/user/order/index.js"
	import {
		getPreOrderById,
	} from "@/api/list/order/index.js"
	import {
		findAllOrders,
		orderReturnApplys
	} from "@/api/user/order/index.js"
	export default {
		data() {
			return {
				navList: [{
						name: "待付款"
					},
					{
						name: "已付款"
					},
					{
						name: "已发货"
					},
					{
						name: "已收货"
					},
					{
						name: "申请退款"
					},
				],
				currentTab: 0,
				orderList: [],
				returnOrderList: [],
			};
		},
		methods: {
			changeTab(index) {
				this.currentTab = index
			},
			confirmPay(orderId) {
				getPreOrderById(orderId).then(res => {
					// console.log(res)
					uni.navigateTo({
						url: "/pages/list/order/order?id=" + orderId
					})
				})
			},
			cancel(orderId) {
				// console.log(orderId)
				cancelOrder(orderId).then(res => {
					// console.log(res)
				})
			},
			doReceive(orderId) {
				receiveDone({
					orderId: orderId
				}).then(res => {
					console.log(res)
				})
			},
			delHistory(orderId) {
				deleteOrder(orderId)
			},
			returnGoods(orderId) {
				uni.navigateTo({
					url: "/pages/mine/myOrder/backOrder/backOrder?id=" + orderId
				})
			}
		},
		computed: {
			list() {
				if (this.currentTab == 4) {
					return this.returnOrderList
				} else {
					return this.orderList.filter(ele => {
						if (this.currentTab == 3) {
							return ele.order.status == 3 || ele.order.status == 4
						}
						return ele.order.status == this.currentTab
					})
				}

			}

		},
		filters: {

			statusText(val) {
				if (val == 0) {
					return "未付款"
				} else if (val == 1) {
					return "确认付款,等待发货"
				} else if (val == 2) {
					return "已发货"
				} else if (val == 3) {
					return "已收货"
				} else if (val == 4) {
					return "已完成"
				} else {
					return "无效订单"
				}

			},
			orderProcessText(process) {
				var text = '等待处理';
				switch (JSON.stringify(process)) {
					case '0':
						text = '等待处理';
						break;
					case '1':
						text = '确认退货,等待用户发货';
						break;
					case '2':
						text = '已退款';
						break;
					case '3':
						text = '已拒绝';
						break;
					case '9':
						text = '拒绝退货';
						break;

					default:
						break;
				}
				return text;
			}
		},
		onLoad(e) {
			console.log(e)
			this.currentTab =e.type
			findAllOrders().then(res => {
				console.log(res, '---------------')
				this.orderList = res.data.orderList
				this.orderList.forEach(ele => ele.items.forEach(e =>
					e.productAttr = JSON.parse(e.productAttr)
				))
				// console.log(this.orderList);
			})
			orderReturnApplys().then(res => {
				console.log(res)
				this.returnOrderList = res.data.items;
				this.returnOrderList.forEach(ele =>
					ele.productAttr = JSON.parse(ele.productAttr))
			})
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		width: 100%;

		.nav {
			display: flex;
			width: 100%;
			position: fixed;
			left: 0;
			top: 87rpx;
			justify-content: space-around;
			align-items: center;
			background-color: #354e44;
			padding-top: 14rpx;
			z-index: 99;

			// height: 76rpx;
			// line-height:76rpx ;
			.nav-item {
				font-size: 30rpx;
				color: #fff;
				padding-bottom: 13rpx;
				border-bottom: 2rpx solid transparent;
			}

			.active {
				border-bottom: 2rpx solid #dd524d;
				color: #dd524d;
			}
		}
	}

	.container-c {
		width: 670rpx;
		margin: 0 auto;
		margin-top: 120rpx;
	}

	.card {
		background-color: #FFFFFF;
		border-radius: 24rpx;
		padding: 36rpx;
		margin-top: 40rpx;

		.top {
			border-bottom: 2rpx solid #f1ece7;
			font-size: 24rpx;
			display: flex;
			justify-content: space-between;
			padding-bottom: 36rpx;
		}

		.middle {
			display: flex;
			align-items: center;
			justify-content: space-between;
			border-bottom: 2rpx solid #f1ece7;
			padding-bottom: 36rpx;

			image {
				width: 162rpx;
				height: 162rpx;
			}

			.right {
				.name{
					width: 280rpx;
					white-space: nowrap;
					text-overflow:ellipsis;
					overflow: hidden;
				}
				.return {
					font-size: 24rpx;
					color: #8d8d8d;
					margin-left: 300rpx;
				}

				// width: 350rpx;
				.top-info,
				.middle-info {
					display: flex;
					justify-content: space-between;
				}

				.top-info {
					font-size: 26rpx;
					line-height: 74rpx;
				}

				.middle-info,
				.bottom-info {
					font-size: 24rpx;
					color: #8d8d8d;
				}
			}
		}
	}
	.bottom {
		display: flex;
		justify-content: space-between;

		.total,
		.time {
			font-size: 26rpx;
			line-height: 65rpx;
		}
	}

	.btn {
		display: flex;
		justify-content: flex-end;
		margin-top: 20rpx;

		.paybtn,
		.cancel,
			{
			width: 128rpx;
			height: 54rpx;
			border: 2rpx solid #2d4f43;
			border-radius: 10rpx;
			font-size: 26rpx;
			line-height: 54rpx;
			text-align: center;
		}

		.cancel {
			margin-right: 20rpx;
		}
	}

	.delete {
		width: 300rpx;
		height: 54rpx;
		border: 2rpx solid red;
		border-radius: 10rpx;
		font-size: 26rpx;
		line-height: 54rpx;
		text-align: center;
		margin-left: 300rpx;
	}
</style>
